<template>
  <div class="resource">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search" />
        <span>筛选搜索</span>
      </div>
      <el-row :gutter="20">
        <el-form
          ref="params"
          :model="params"
          label-width="100px"
          size="small"
          class="searchForm"
        >
          <el-col :span="6">
            <el-form-item label="材料名称:" prop="materialName">
              <!-- <el-select v-model="params.enterpriseType" placeholder="材料名称" clearable size="small">
                <el-option
                    v-for="dict in materialSciences"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                    />
              </el-select> -->
              <el-input
                v-model="params.materialName"
                size="small"
                clearable
                placeholder="请输入材料名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button
              @click="search"
              icon="el-icon-search"
              size="small"
              type="cyan"
              >搜索</el-button
            >
          </el-col>
        </el-form>
      </el-row>
    </el-card>
    <div class="container">
      <div class="handle-box">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="mr10"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </div>

      <!-- <el-card shadow="hover">
        <h4 style="margin-bottom: 10px">咨询公司管理</h4> -->
        <el-table
          max-height="550"
          v-loading="loading"
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column
            type="selection"
            width="60"
            align="center"
          ></el-table-column> -->
          <el-table-column
            label="材料名称"
            align="center"
          >
          <template slot-scope="scope">{{scope.row.materialName}}</template>
          </el-table-column>
          <el-table-column
            prop="example"
            label="附件下载"
            align="center"
            min-width="150"
          >
          <template slot-scope="scope" v-if="scope.row.example">
            <el-link icon="el-icon-download" :href="showUrl+scope.row.example">下载</el-link>
          </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" min-width="160">
            <template slot-scope="scope">{{ scope.row.createTime }}</template>
          </el-table-column>
          <el-table-column
            label="操作"
            min-width="230"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
                >修改</el-button
              >
              <!-- <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
      <!-- </el-card> -->
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="800px">
      <el-form
        :rules="rules2"
        ref="formData"
        :model="formData"
        label-width="120px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="18">
            <el-form-item label="材料名称" prop="materialName">
              <el-input
                v-model="formData.materialName"
                size="small"
                clearable
                placeholder="请输入材料名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="材料样例" prop="fileType">
              <editor v-model="formData.fileType" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="附件上传">
                  <!-- accept=".doc,.xlsx" -->
              <el-upload
                  class="upload-demo"
                  :headers="headers"
                  :action="`${baseUrl}/goods/order/file/upload`"
                  :on-remove="handleRemove"
                  :limit="1"
                  :on-success="uploadLogoPicUrl"
                  >
                  <div v-if="formData.id">
                    <el-button size="small" type="primary" >重新上传</el-button>
                    <el-link style="margin-left:10px" icon="el-icon-download" :href="showUrl+formData.example">下载附件</el-link>
                  </div>
                  <el-button size="small" type="primary" v-else>点击上传</el-button>
                  <!-- <div slot="tip" class="el-upload__tip">只能上传.doc,.xlsx文件</div> -->
                </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取消</el-button>
        <el-button @click="submitForm()" type="primary" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  add,
  put,
  del,
  srh
} from "@/api/materialScience/materialScience";
import { getToken } from "@/utils/auth";
import Editor from '@/components/Editor';
export default {
  components: {
    Editor
  },
  props: [],
  data() {
    return {
      picurls:[],
      photoList:[],
      dialogVisible_img:false,
      dialogImageUrl:'',
      
      baseUrl: process.env.VUE_APP_BASE_API,
      headers: { Authorization: "Bearer " + getToken() },
      checkedGroups:[],
      companyTypes:[],
      certificationSystems:[],
      materialSciences:[],
      // 非多个禁用
      multiple: true,
      //删除
      ids: [],
      orderNum_show: false,
      examine: false,
      title666: "",
      rules2: {
        materialName: [{ required: true, message: "请输入材料名称", trigger: "blur" }],
        fileType: [
          { required: true, message: "请上传样例", trigger: "blur" },
        ],
      },
      input: "",
      openFlag: true,
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
      },
      pageTotal: 0,
      dialogVisible: false, // 是否展示编辑页面
      // 新增/编辑数据
      formData: {
        materialName: "",
        example:""
      },
      customer: {
        customerId: "",
      },
      paramsFlag: 0,
      editFlags: false,
    };
  },
  computed: {},
  created() {
    this.getData00();
    let arr8 = [1,8,9,5,6,4,2,11,13,18,16,20,22,24];
    for(let i = 0;i<arr8.length;i++){
      this.steps.forEach(item=>{
        if(item.desc==arr8[i]){
          return item.show='2';
        }
      })
    };
    console.log(this.steps);
  },
  mounted() {
    //   获取公司类型
    // this.getDicts("company_type").then(response => {
    //   this.companyTypes = response.data;
    // });
    // 获取认证体系
    // this.getDicts("certification_System").then(response => {
    //   this.certificationSystems = response.data;
    // });
    // 获取材料列表
    // this.getDicts("materialSciences").then(response => {
    //   this.materialSciences = response.data;
    // });
  },
  watch: {},
  methods: {
    // 监听单选框
    radioChange(e){
      console.log(e);
      // 切换时清空材料
      this.formData.example=''
    },
    uploadLogoPicUrl(res) {
      this.$message.success(`上传成功`);
      this.formData.example = res.data;
      console.log(this.showUrl+this.formData.example)
    },
    handleRemove(file, fileList){
      console.log(file, fileList);
      this.formData.example=''
    },
    // 多选框监测
    handleCheckedChange(e){
        // console.log(e);
        this.formData.materialName=e
    },
    // 键值配对
    certificationSystems_type(val) {
      var str = this.certificationSystems.find(function (item) {
        return item.dictValue == val;
      });
      if (str) return str.dictLabel;
    },
    comany_type(val) {
      var str = this.companyTypes.find(function (item) {
        return item.dictValue == val;
      });
      if (str) return str.dictLabel;
    },
    // 新增修改的方法
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (this.title666 == "新增") {
            //   let a = this.formData.materialName.join(',');
            //   if(a[a.length-1] == ","){
            //     this.formData.materialName=a.substring(0,a.length-1);
            //   }else{
            //     this.formData.materialName=a;
            //     }
            // this.formData.materialName = JSON.stringify(this.formData.materialName);
            if(this.formData.example){
              add(this.formData).then((res) => {
                console.log(res);
                if (res.code == 200) {
                  this.$message.success(`新增成功`);
                  this.dialogVisible = false;
                  this.getData00();
                }
              });
            }else{
              this.$message.error(`请完善材料样例`);
            }
          } else {
            // this.formData.materialName = JSON.stringify(this.formData.materialName);
            if(this.formData.example){
              put(this.formData).then((res) => {
                if (res.code == 200) {
                  this.$message.success(`修改成功`);
                  this.dialogVisible = false;
                  this.uploading = false;
                  this.getData00();
                }
              });
            }else{
              this.$message.error(`请完善材料样例`);
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    reset(){
      console.log('此处清除表达数据');
      this.formData={
        materialName: "",
        example:""
      }
    },
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
      this.title666 = "新增";
    },
    handleEdit(index, row) {
    //   this.formData = row;
      srh(row.id).then((res) => {
        if (res.code == "200") {
          this.dialogVisible = true;
          this.$nextTick(() => {
            this.formData = res.data;
          })
          // this.checkedGroups=JSON.parse(res.data.materialName);
          // this.orderNum_show = true;
          // this.examine = false;
          this.title666 = "编辑";
        }
      });
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map((item) => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return del(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    //获取数据
    getData00() {
      list(this.params).then((res) => {
        if (res.code == "200") {
          this.tableData = res.rows;
          this.pageTotal = res.total;
          this.loading = false;
        }
      });
    },
    // 触发搜索按钮
    search() {
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    },
  },
};
</script>

<style scoped>
.resource {
  padding: 30px;
}
.container {
  margin-top: 20px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
  
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.compangImg {
  display: block;
  width: 280px;
  height: 200px;
}
.study_pic {
  display: block;
  width: 60px;
  height: 60px;
  margin: auto;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}


.progress_box{
  width: 100%;
  display: flex;
  box-sizing: border-box;
  padding: 10px 30px;
  margin: 0 auto;
  justify-content: space-between;
  background-color: #fff;
  /* align-items: center; */
}
.progress_box .left{
  width: 15%;
  color: #888;
  font-size: 14px;
}
.progress_box .left_c{
  width: 15%;
  color: #444;
  font-weight: bold;
  font-size: 14px;
}
.progress_box .right{
  width: 75%;
  color: #888;
  font-size: 14px;
}
.progress_box .right_c{
  width: 75%;
  color: #444;
  font-size: 14px;
}
.progress_box .center{
  width: 10%;
  text-align: center;
  line-height: 1;
  /* display: flex;
  align-items: center; */
}
.top88{
  display: flex;
  align-items: center;
}
.title88{
  display: flex;
  align-items: center;
  margin: 10px 0;
  font-weight: bold;
}
.desc88{
  margin-bottom: 10px;
}
.loading88{
  color: #3D6FFF;
  margin-left: 40px;
}
</style>
